import {
  SIGN_IN_REQUEST,
  SIGN_IN_SUCCESS,
  SIGN_IN_FAILED,
  RESET_LOGIN_TIPS,
} from '../constance/actionType';

import {
  PAGE_INIT,
  PAGE_LOADING,
  PAGE_LOADED
} from '../constance/pageType';

import _ from 'lodash';
const initState = {
  pageState: PAGE_INIT,
  errorLevel: -1,
  errorMsg: ''
};

export default function user(state=initState, action) {
  const { type, userInfo, errorMsg } = action;
  switch( type ) {
    case RESET_LOGIN_TIPS:
      return _.assign({}, state, {
        errorLevel: -1,
        errorMsg: ''
      });
    case SIGN_IN_REQUEST:
      return _.assign({}, state, {
        pageState: PAGE_LOADING
      });
    case SIGN_IN_FAILED:
      return _.assign({}, state, {
        errorLevel: 0,
        errorMsg: errorMsg || '用户名/密码错误',
        pageState: PAGE_LOADED
      });
    case SIGN_IN_SUCCESS:
      return _.assign({}, state, {
        pageState: PAGE_LOADED
      });
    case 'LOCATION_CHANGE':
      console.log('LOCATION_CHANGE')
      console.log(action);
      return state;
    default:
      return state;
  }
}
